/*
 * @lc app=leetcode.cn id=83 lang=javascript
 *
 * [83] 删除排序链表中的重复元素
 *
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/
 *
 * algorithms
 * Easy (46.94%)
 * Likes:    183
 * Dislikes: 0
 * Total Accepted:    37.6K
 * Total Submissions: 80K
 * Testcase Example:  '[1,1,2]'
 *
 * 给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。
 *
 * 示例 1:
 *
 * 输入: 1->1->2
 * 输出: 1->2
 *
 *
 * 示例 2:
 *
 * 输入: 1->1->2->3->3
 * 输出: 1->2->3
 *
 */
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
  function ListNode(val) {
    this.val = val;
    this.next = null;
  }

  if (!head) {
    return head;
  }

  let ret = new ListNode(null);
  let pre = ret;
  let preVal = null;

  while (head) {
    let val = head.val;

    // 注意输入的链表是已排序的
    if (preVal !== val) {
      pre.next = new ListNode(val);
      pre = pre.next;
      preVal = val;
    }

    head = head.next;
  }

  return ret.next;
};
